package com.google.bitcoin.core;

import com.google.bitcoin.script.*;

import static com.google.bitcoin.script.ScriptOpCodes.*;

/**
 * Represents an expression that can be evaluated to determine the correct address that can redeem
 * an oracle transaction.
 */
public class OracleBinaryContractExpression {

    byte[] trueNonceHash;
    byte[] falseNonceHash;
    byte[] truePubKey;
    byte[] falsePubKey;

    NetworkParameters params;
    private int timeComplete;

    public OracleBinaryContractExpression(NetworkParameters params,
                                          byte[] trueNonceHash, byte[] falseNonceHash, byte[]truePubKey, byte[] falsePubKey, int timeComplete) {
        this.params = params;
        this.trueNonceHash = trueNonceHash;
        this.falseNonceHash = falseNonceHash;
        this.truePubKey = truePubKey;
        this.falsePubKey = falsePubKey;
        this.timeComplete = timeComplete;
    }

    public Script getScript() {
        ScriptBuilder sb = new ScriptBuilder();
        sb.op(OP_HASH160);
        sb.op(OP_DUP);
        sb.data(trueNonceHash);
        sb.op(OP_EQUAL);
        sb.op(OP_IF);
        sb.op(OP_DROP);
        sb.data(truePubKey);
        sb.op(OP_CHECKSIG);
        sb.op(OP_ELSE);
        sb.data(falseNonceHash);
        sb.op(OP_EQUAL);
        sb.op(OP_IF);
        sb.data(falsePubKey);
        sb.op(OP_CHECKSIG);
        sb.op(OP_ELSE);
        sb.smallNum(2);
        sb.data(truePubKey);
        sb.data(falsePubKey);
        sb.smallNum(2);
        sb.op(OP_CHECKMULTISIG);
        sb.op(OP_ENDIF);
        sb.op(OP_ENDIF);
        return sb.build();
    }

    public int getEventCompleteTimestamp() {
        return timeComplete;
    }
}
